<?php

	function getPreviousNextPageText($Nbpages)
	{
		
		$text['next_page_text'] = "";
		$text['previous_page_text'] = "";
			
		if (isset($_SESSION['page']))
		{
			
			if ($_SESSION['page'] > 1)
			{
				$text['previous_page_text'] = "	<form style='float:left; margin:0;'>
											<input type='hidden' name='page' value='".($_SESSION['page']-1)."'>";
											
				foreach ($_GET as $name => $value)
				{
					if ($name != "page")
					{
						$text['previous_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
					}
				}
											
				$text['previous_page_text'] .= "	<input type='submit' value ='Page ".($_SESSION['page']-1)."'>
										</form>";
			}
			if ($_SESSION['page'] < $Nbpages)
			{
				$text['next_page_text'] = "	<form style='float:right; margin:0;'>
										<input type='hidden' name='page' value='".($_SESSION['page']+1)."'>";
										
				foreach ($_GET as $name => $value)
				{
					if ($name != "page")
					{
						$text['next_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
					}
				}
				
				
				$text['next_page_text'] .= "	<input type='submit' value ='Page ".($_SESSION['page']+1)."'>
									</form>";
			}
		}
		else
		{
			if ($Nbpages > 1)
			{
				$text['next_page_text'] = "<form style='float:right; margin:0;'><input type='hidden' name='page' value='2'><input type='submit' value ='Page 2'>";
				
				foreach ($_GET as $name => $value)
				{
					if ($name != "page")
					{
						$text['next_page_text'] .= '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
					}
				}
				
				$text['next_page_text'] .= "</form>";
			}
		}
	
		return $text;
	
	}

	function useAppropriateRedirection()
	{
		
		if ((!empty($_SESSION['search_filters']) && isset($_GET['tri'])) || (isset($_GET['page'])))
		{
			if (isset($_GET['page']))
			{
				unset($_GET['page']);
			}
			header("Location:locations.php");
		}
	
	}

	function getActualPage()
	{
		
		if (isset($_GET['page']) && isint($_GET['page']))
		{
			$_SESSION['page'] = $_GET['page'];
		}
		elseif (!isset($_SESSION['page']))
		{
			$_SESSION['page'] = 1;
		}
		
	}

	function makeNewFilters()
	{
			
		$_SESSION['search_filters'] = array();
		
		if (validcountry($_GET['pays']))
		{
			$_SESSION['search_filters']["bien.ref_adresse"] = "adresse.id_adresse";
			$_SESSION['search_filters']["adresse.pays"] = "'".$_GET['pays']."'";
		}
		if (validcategory($_GET['cat']))
		{
			$_SESSION['search_filters']["bien.categorie"] = "'".$_GET['cat']."'";
		}
		if (!(empty($_GET['nbpers'])) && isint($_GET['nbpers']))
		{
			$_SESSION['search_filters']["bien.capacite"] = "'".$_GET['nbpers']."'";
		}
		
		if (!(empty($_GET['prixmin'])) && !(empty($_GET['prixmax'])) && isint($_GET['prixmin']) && isint($_GET['prixmax']) && $_GET['prixmin'] > 0 && $_GET['prixmax'] > 0)
		{
			
			if ($_GET['prixmin'] > $_GET['prixmax'])
			{
				$tmp = $_GET['prixmin'];
				$_GET['prixmin'] = $_GET['prixmax'];
				$_GET['prixmax'] = $tmp;
			}
			
			$_SESSION['search_filters']["location.prixmin"] = "'".$_GET['prixmin']."'";
			$_SESSION['search_filters']["location.prixmax"] = "'".$_GET['prixmax']."'";
		
		}
		
		$_SESSION['page'] = 1;
		
	}
	
	function newVisit()
	{
		if (isset($_SERVER["HTTP_REFERER"]))
		{
			$last_path = explode($_SERVER["SERVER_NAME"],$_SERVER["HTTP_REFERER"]);
			$last_path = $last_path[1];
		}
		else
		{
			$last_path = "";
		}
		return $last_path != $_SERVER["PHP_SELF"] || !isset($_SESSION['search_filters']);
	}

	function GetActiveCountries($BDD_connexion)
	{

		$countries = array();

		$request = "SELECT DISTINCT pays FROM adresse";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->execute();
		while($country=$prepare->fetch(PDO::FETCH_OBJ))
		{
			$countries[] = $country->pays;
		}

		return $countries;
	}

	function GetActiveCategories($BDD_connexion)
	{

		$categories = array();

		$request = "SELECT DISTINCT categorie FROM bien";
		$prepare=$BDD_connexion->prepare($request);
		$prepare->execute();
		while($category=$prepare->fetch(PDO::FETCH_OBJ))
		{
			$categories[] = $category->categorie;
		}

		return $categories;
	}

	function GetLocationsThroughFilters($BDD_connexion,$filters,$page,$LocationsByPage)
	{
		$locations = array();

		$request = "SELECT DISTINCT * FROM bien,adresse,location WHERE location.ref_bien = bien.id_bien";

		if (!empty($filters))
		{
			$request.= " AND ";
			foreach ($filters as $filter => $filtervalue)
			{
				switch ($filter) {
					case "bien.capacite" :
						$request.= " ".$filter." >= ".$filtervalue;
						break;
					case "location.prixmin" :
						$request.= " location.promo >= ".$filtervalue;
						break;
					case "location.prixmax" :
						$request.= " location.promo <= ".$filtervalue;
						break;	
					default:
						$request.= " ".$filter." = ".$filtervalue;
				}
				$request.= " AND";
			}
			$request = substr($request, 0, -3);
		}
		
		$request .= " GROUP BY bien.ref_adresse";
		$request .= " LIMIT ".($page*$LocationsByPage).",".((($page+1)*$LocationsByPage));

		$prepare=$BDD_connexion->prepare($request);		
		$prepare->execute();
		while($location=$prepare->fetch(PDO::FETCH_OBJ))
		{
			$location->description = getLocationDescription($BDD_connexion,$location->id_bien);
			$location->notation= getLocationNotation($BDD_connexion,$location->id_bien);
			$locations[] = $location;
		}

		return $locations;
	}
	
	function validcountry($value)
	{
		$connect = PDO_BDD_Connect();
		$countries = GetActiveCountries($connect);
		return in_array($value,$countries);
	}
	
	function validcategory($value)
	{
		$connect = PDO_BDD_Connect();
		$categories = GetActiveCategories($connect);
		return in_array($value,$categories);
	}
	
	function getTotalNbPagesThroughFilters($BDD_connexion,$filters,$LocationsByPage)
	{
		
		$count = "SELECT COUNT(DISTINCT ref_adresse) as count FROM bien,adresse,location WHERE location.ref_bien = bien.id_bien";
		$total = 0;

		if (!empty($filters))
		{
			$count.= " WHERE ";
			foreach ($filters as $filter => $filtervalue)
			{
				switch ($filter) {
					case "bien.capacite" :
						$count.= " ".$filter." >= ".$filtervalue;
						break;
					default:
						$count.= " ".$filter." = ".$filtervalue;
				}
				$count.= " AND";
			}
			$count = substr($count, 0, -3);
		}
		
		$preparecount = $BDD_connexion->prepare($count);
		$preparecount->execute();
		$nombre=$preparecount->fetch(PDO::FETCH_OBJ);
		
		if ($nombre != false)
		{
			$total = ($nombre->count - $nombre->count % $LocationsByPage) / $LocationsByPage;

			if ((($nombre->count % $LocationsByPage) > 0))
			{
				$total += 1;
			}
		}
		
		if ($total == 0)
		{
			$total++;
		}
		
		return $total;
		
	}
	

?>
